﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Job.Logic.Personal;
using Job.Entity;
using Job.Web.Framework;

namespace Job.Web.userControls.Personal
{
    public partial class Education : BaseCtrl
    {
        int eduid = myR["eduid"].IntValue;
        int subjectid = myR["subjectid"].IntValue;
        int pid = myR["resumeid"].IntValue;
        PersonalLogic pl = new PersonalLogic();
        JobPersonEdu edu = new JobPersonEdu();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.InitData();
                this.LoadData();
            }
        }

        private void ShowMessage(string message)
        {
            this.message.InnerHtml = "<font color=red>" + message + "</font>";
            this.message.Visible = true;
            this.message.Height = "30px";
            this.message.VAlign = "middle";
        }

        private void InitData()
        {
            //初始化月份
            ddlFMonth.Items.Clear();
            ddlFYear.Items.Clear();
            ddlTYear.Items.Clear();
            ddlTMonth.Items.Clear();

            for (int i = 1; i <= 12; i++)
            {
                ddlFMonth.Items.Add(new ListItem(i.ToString(), i.ToString()));
                ddlTMonth.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }

            //初始化年份
            for (int i = 1949; i <= DateTime.Now.Year; i++)
            {
                ddlFYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
                ddlTYear.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }

            WebCommon.SetDropDownFromDict(ddlHeight, "", Constants.GetDictionaryCode(Constants.DictionaryType.EDUCATION_LEVEL), true);
        }

        private void LoadData()
        {
            try
            {
                gvedu.DataSource = pl.GetEdusByPersonalId(pid);
                gvedu.DataBind();

                if (eduid > 0)
                {
                    edu = pl.GetSingleEdu(eduid);
                    if (edu != null)
                    {
                        txtSchooleName.Text = edu.SchooleName;
                        txtsName.Text = edu.SpecialtyName;
                        txtDesc.Text = edu.EduDesc;
                        if (edu.StartYear.HasValue)
                        {
                            if (ddlFYear.Items.FindByValue(edu.StartYear.Value.ToString()) != null)
                            {
                                ddlFYear.SelectedValue = edu.StartYear.Value.ToString();
                            }
                        }

                        if (edu.StartMonth.HasValue)
                        {
                            if (ddlFMonth.Items.FindByValue(edu.StartMonth.Value.ToString()) != null)
                            {
                                ddlFMonth.SelectedValue = edu.StartMonth.Value.ToString();
                            }
                        }

                        if (edu.EndYear.HasValue)
                        {
                            if (ddlTYear.Items.FindByValue(edu.EndYear.Value.ToString()) != null)
                            {
                                ddlTYear.SelectedValue = edu.EndYear.Value.ToString();
                            }
                        }

                        if (edu.EndMonth.HasValue)
                        {
                            if (ddlTMonth.Items.FindByValue(edu.EndMonth.Value.ToString()) != null)
                            {
                                ddlTMonth.SelectedValue = edu.EndMonth.Value.ToString();
                            }
                        }

                        //学历
                        string highEdu = string.Empty;
                        if (edu.EduHeight.HasValue)
                            highEdu = edu.EduHeight.Value.ToString();

                        if (ddlHeight.Items.FindByValue(highEdu) != null)
                        {
                            ddlHeight.SelectedValue = highEdu;
                        }


                    }//end work = null
                    lblMode.Text = "编辑教育经历";
                }//end work id >0
                else
                {
                    lblMode.Text = "新增教育经历";
                }
            }
            catch (Exception ex)
            {
                ShowMessage("查询失败：<br>" + ex.Message);
                Logger.WriteLog("教育经历查询失败：" + ex.Message);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (edu == null)
                {
                    edu = new JobPersonEdu();
                }

                edu.SchooleName = txtSchooleName.Text.Trim();
                edu.SpecialtyName = txtsName.Text.Trim();
                edu.EduDesc = txtDesc.Text.Trim();

                edu.StartYear = Convert.ToInt32(ddlFYear.SelectedValue);
                edu.StartMonth = Convert.ToInt32(ddlFMonth.SelectedValue);
                edu.EndYear = Convert.ToInt32(ddlTYear.SelectedValue);
                edu.EndMonth = Convert.ToInt32(ddlTMonth.SelectedValue);

                edu.EduHeight = Convert.ToInt32(ddlHeight.SelectedValue);
                edu.PersonInfoId = pid;

                if (eduid > 0)
                {
                    edu.PersonEduId = eduid;
                    pl.UpdateEdu(edu);
                }
                else
                {
                    edu.PersonInfoId = pid;
                    pl.InsertEdu(edu);
                }
            }
            catch (Exception ex)
            {
                ShowMessage("保存失败：<br>" + ex.Message);
                Logger.WriteLog("教育经历保存失败：" + ex.Message);
                return;
            }
            string url = WebCommon.GetApplicationPath(Request) + "Person/PersonalResume.aspx?categoryid=1&subjectid=" + this.subjectid + "&resumeid=" + this.pid;
            Response.Redirect(url);
        }

        protected void gvWork_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName.ToLower() == "edit")
            {
                //edit
                string url = WebCommon.GetApplicationPath(Request) + "Person/PersonalResume.aspx?categoryid=1&subjectid=" + this.subjectid + "&resumeid=" + this.pid + "&eduid=" + id;
                Response.Redirect(url);

            }
            else if (e.CommandName.ToLower() == "delete")
            {
                JobPersonEdu _edu = pl.GetSingleEdu(id);
                pl.DeleteEdu(_edu);

                string url = WebCommon.GetApplicationPath(Request) + "Person/PersonalResume.aspx?categoryid=1&subjectid=" + this.subjectid + "&resumeid=" + this.pid;
                Response.Redirect(url);
            }
        }

        protected void gvWork_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }
    }
}